
const db=wx.cloud.database() 
var prize
const app=getApp()
Page({
    data: {
      isDoudong: false, //控制图片抖动
      prize:{},
      isPrize: false,
      pupList1:false,
      showPupup2: false,
      pupList2:false,
      showPrize:false,
      currentPrize:[],
      index:false
    },
    enterRecord(){
      wx.navigateTo({
        url: '../prizeDetail/prizeDetail',
      })
    },
    pointMe:function(){
      let that = this
      let isDoudong = that.data.isDoudong
      that.setData({
        isDoudong: !isDoudong
      })
      // if(isDoudong==false)
      // {
      const innerAudioContext = wx.createInnerAudioContext()
      innerAudioContext.autoplay = true
      innerAudioContext.src='cloud://cloud1-5gp2csoo37e7e178.636c-cloud1-5gp2csoo37e7e178-1307826028/14203.wav	'
      innerAudioContext.onPlay(() => {
      console.log("Start playing.")
      })
      innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
      })
    setTimeout(()=>this.getBack(),3000 );// 第一个参数也可为this.getBack.bind(this)
    // setTimeout(()=>this.getPrize(),2000 );
    setTimeout(()=>this.randomPrize(),2000 );
  },
  getBack(){
    let that = this
    let isDoudong = that.data.isDoudong
    that.setData({
      isDoudong:false
    })
  },
  getPrizeList (){
    return db.collection('prize')
    .get()
    .then(res=>{
      console.log("成功",res)
      this.setData({
        prize: res.data
      })
      prize=this.data.prize
    })
   
  },
  async randomPrize(){
   await this.getPrizeList()
    console.log(prize)
    var prob=[];
    for (var j = 0; j <prize.length; j++) {
    prob[j]=prize[j].renumber
  }
  console.log(prob)
   var Index1 = Math.random() //随机产生数
   console.log(Index1)
  var Index2=0
  var rotat= 0
  // 计算权值
  var i = 0
  var j
  var index
  console.log("随机数",Index1)
  for ( j= 0; j < prob.length; j++) {
    if (i==0){
      console.log("j",j)
      rotat = rotat+ prob[j] 
      console.log("tota",rotat)
      if (Index1 <= rotat){
        console.log(j)
        console.log(this.data.prize[j].prizename)
        console.log(this.data.prize[j].renumber)
        var prize11=this.data.currentPrize
        prize11=[{
          name:this.data.prize[j].prizename,
          image:this.data.prize[j].image,
          "num":this.data.prize[j].renumber
        }]
        this.setData({
          currentPrize:prize11,
          showPrize:true,
        })
          if(j!=5)
          {
           this.updatePrizeRecord(this.data.prize[j].prizename)
        i=i+1
    }
    }
  }
  }
},
 updatePrizeRecord(name){
  wx.cloud.callFunction({
    name:"updateUserInfo",
    data:{
      time:app.getTime(5),
      prizeName:name,
      action:"prize",
      openid:app.globalData.openid
    }
  })
  .then(res=>{
    console.log("更新奖品成功")
  })
},
 
  showPupup2() {
    this.setData({
      showPupup2: true
    })
  },
  //关闭抽奖弹窗
  onClose2() {
    this.setData({
      showPupup2: false,
      pupList2: false
    })
  },
    //展示抽奖清单
    pupList2() {
      this.setData({
        pupList2: true
      })
    },
    showPupup1() {
      this.setData({
        showPupup1: true
      })
    },
    //关闭抽奖弹窗
    onClose1() {
      this.setData({
        showPrize: false,
        pupList1: false
      })
    },
      //展示抽奖清单
      pupList1() {
        this.setData({
          pupList1: true
        })
      },
      onLoad(){
     this.getPrizeList()
      },
  })